<?php

if(!defined("Yourphp")) exit("Access Denied");
class WritingAction extends BaseAction
{
    function _initialize()
    {	
		parent::_initialize();
		if(!$this->_userid){
			$this->assign('jumpUrl',U('User/Login/index'));
			$this->error(L('nologin'));
			exit;
        }
    }
    
    public function index() {
        $list = M('zhuanye')->field('id,title')->select();
        $this->assign('list', $list);
        $this->display();
    }
    
    //获取学校列表
    public function getschool(){
       $list =  M('access_zhuanye')->field('uid,zyid')->where(array('schoolid'=>0,'zyid'=>$_POST['aa']))->select();
      foreach($list as $key=>$value){
          $list[$key]['sclist'] = M('user')->field('id,realname')->where(array('id'=>$value['uid']))->select();
      }
      echo json_encode($list);
       exit ;
    }
    
    public function getexam() {
        $ee = $_POST['ee'];
        $list = M('access_zhuanye')->field('uid')->where(array('schoolid' => $ee))->select();
        foreach ($list as $key => $value) {
            $aa[] = $value['uid'];
        }
        $cc = implode(',', $aa);
        $map['userid'] = array('in', $cc);
        $map['type'] = 1;
        $info = M('exam')->where($map)->select();
        foreach ($info as $kk => $vv) {
            $info[$kk]['createtime'] = date('Y-m-d H:i:s', $vv['createtime']);
        }
        echo json_encode($info);
        exit;
    }

    public function info() {
        $id = $_GET['id'];
        $exam = M('exam')->where(array('id' => $id))->find();
        $arr = json_decode($exam['question'], true);
        $cc = implode(',', $arr['que']);
        $map['eid'] = array('in', $cc);
        $map['type'] = 5;
        if(empty($_GET['studentid'])){
            $info = M('exam_record')->field('id,zg_uploads,zg_score,user_id,answers,laud')->where(array('eid' => $id))->order('id desc')->limit(1)->select(); //获取一名学生的考试信息
        }else{
            $info = M('exam_record')->field('id,zg_uploads,zg_score,user_id,answers,laud')->where(array('eid' => $id,'user_id'=>$_GET['studentid']))->order('id desc')->select(); //获取一名学生的考试信息
        }
        $student = M('user')->field('id,realname')->where(array('id' => $info[0]['user_id']))->find(); //获取学生信息
        $this->assign('student', $student); //获取学生信息
        $zyname = M('zhuanye')->field('id,title')->where(array('id' => $_GET['zyid']))->find(); //获取专业信息
        $this->assign('zyname', $zyname); //获取专业信息
        $answerinfo = json_decode($info[0]['answers'], true);  //本场考试学生的答案信息
        foreach ($answerinfo as $kk => $vv) {
            $stuexam = M('question_bank')->where(array('id' => $kk, 'type' => 5))->find();   //获取这个学生的主观题考试信息
            if (!empty($stuexam)) {
                $list[$kk] = $stuexam;
                $list[$kk]['daan'] = $vv ;
                $list[$kk]['pinglun']  = M('comment_company')->query("select cc.*,u.realname from yourphp_comment_company as cc  left join yourphp_user as u on cc.companyid = u.id where cc.examrecordid = '".$info[0]['id']."' and cc.questionid='".$stuexam['id']."' and cc.studentid = '".$student['id']."'") ;
            }
        }
        $typedianzan = M('laud')->where(array('user_id'=>$this->_userid,'exid'=>$info[0]['id']))->find() ;
        if(!empty($typedianzan)){
            $this->assign('dianzan', 1);
        }else{
            $this->assign('dianzan', 2);
        }
        $this->assign('info', $info[0]);
        $this->assign('list', $list);
        $this->assign('id', $id);
        $this->assign('arr', $arr);
        $this->display();
    }
    
    //评论
    public function comment() {
        $data['examrecordid'] = $_POST['answerid'];
        $data['questionid'] = $_POST['questionid'];
        $data['studentid'] = $_POST['studentid'];
        $data['comment'] = $_POST['comment'];
        $data['companyid'] = $this->_userid;
        $data['dateline'] = time();
        $cid = M('comment_company')->add($data);
        if ($cid) {
            echo json_encode(array('code' => '0000', 'msg' => '添加成功'));
            exit;
        } else {
            echo json_encode(array('code' => '0001', 'msg' => '添加失败'));
            exit;
        }
    }
    
    //其他学生
    public function other() {

        $list = M('exam_record')->field('user_id,total_score')->where(array('user_id' => array('neq', $_GET['studentid']), 'eid' => $_GET['id']))->select();
        foreach ($list as $kk => $vv) {
            $stuexam = M('user')->field('id,realname,username')->where(array('id' => $vv['user_id']))->find();   //获取这个学生的主观题考试信息
            if (!empty($stuexam)) {
                $list[$kk]['stu'] = $stuexam;
            }
        }

        $this->assign($_GET);
        $this->assign('list', $list);
        $this->display();
    }

}

?>